package com.ruoyi.scope.controller;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.scope.domain.Scope;
import com.ruoyi.scope.service.ScopeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 经营范围基础Controller
 *
 * @author zjh
 * @date 2022-04-08
 */
@Controller
@RequestMapping("/scope")
public class ScopeController extends BaseController {
    private String prefix = "scope";

    @Autowired
    private ScopeService scopeService;

    @RequiresPermissions("business:scope:view")
    @GetMapping()
    public String scope()
    {
        return prefix + "/scope";
    }

    /**
     * 查询经营范围基础列表
     */


    @RequiresPermissions("@ss.hasPermi('enterprise:scope')")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(Scope scope)
    {
        startPage();
        List<Scope> list = scopeService.selectScopeList(scope);
        return getDataTable(list);
    }

    /**
     * 导出经营范围基础列表
     */
    @PreAuthorize("@ss.hasPermi('business:scope:view')")
    @Log(title = "经营范围基础", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Scope scope)
    {
        List<Scope> list = scopeService.selectScopeList(scope);
        ExcelUtil<Scope> util = new ExcelUtil<Scope>(Scope.class);
        return util.exportExcel(list, "经营范围基础数据");
    }

    /**
     * 新增经营范围基础
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存经营范围基础
     */
    @PreAuthorize("@ss.hasPermi('enterprise:scope')")
    @Log(title = "经营范围基础", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody  Scope scope)
    {
        SysUser sysUser= SecurityUtils.getLoginUser().getUser();
        Long count = scopeService.selectScopeIdByNameAndProductRemark(scope.getScopeClass(), scope.getProductClass(), scope.getProductRemark());
        if (count != null){
            return AjaxResult.error("请勿重复添加信息!");
        }
        scope.setCreatBy(sysUser.getNickName());
        return toAjax(scopeService.insertScope(scope));
    }

    /**
     * 修改经营范围基础
     */
    @GetMapping("/edit/{scopeId}")
    public String edit(@PathVariable("scopeId") Long scopeId, ModelMap mmap)
    {
        Scope scope = scopeService.selectScopeById(scopeId);
        mmap.put("scope", scope);
        return prefix + "/edit";
    }

    /**
     * 修改保存经营范围基础
     */
   // @PreAuthorize("@ss.hasPermi('business:scope:view')")
    @Log(title = "经营范围基础", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody Scope scope)

    {
        SysUser sysUser= SecurityUtils.getLoginUser().getUser();
        Long myid = scopeService.selectScopeIdByNameAndProductRemark(scope.getScopeClass(),scope.getProductClass(),scope.getProductRemark());
        if ((myid!=null)&&(!myid.equals(scope.getScopeId()))){
            return AjaxResult.error("请勿重复添加信息!");
        }
        scope.setUpdateBy(sysUser.getUserName());
        return toAjax(scopeService.updateScope(scope));
    }

    /**
     * 删除经营范围基础
     */
  //  @PreAuthorize("@ss.hasPermi('business:scope:view')")
  //  @Log(title = "经营范围基础", businessType = BusinessType.DELETE)
    @GetMapping( "/remove/{scopeId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("scopeId") String scopeId)
    {
        return toAjax(scopeService.deleteScopeByIds(scopeId));
    }


    /**
     * 导入经营范围基础
     */
//    @Log(title = "经营范围基础", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('business:scope:view')")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<Scope> util = new ExcelUtil<Scope>(Scope.class);
        List<Scope> scopeList = util.importExcel(file.getInputStream());
        String message = scopeService.importScope(file,scopeList, SecurityUtils.getLoginUser().getUser().getUserName());
        return AjaxResult.success(message);
    }

   // @PreAuthorize("@ss.hasPermi('business:scope:view')")
    @GetMapping("/exportTemplate")
    @ResponseBody
    public void exportTemplate(HttpServletRequest request, HttpServletResponse response) throws IOException
    {
        // 经营范围信息模板
        String s = scopeService.exportTemp();
        String downloadName = StringUtils.substringAfterLast(s, "/");
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, downloadName));
        FileUtils.writeBytes(s, response.getOutputStream());
        FileUtils.deleteFile(s);
        ExcelUtil<Scope> util = new ExcelUtil<Scope>(Scope.class);
    }
}
